const Koa= require("koa");
const router = require("./routes");
const koaJwt = require('koa-jwt');
const app = new Koa();
const cors = require("koa2-cors");
const bodyParser = require("koa-bodyparser");
const koaBody = require('koa-body')
const static = require('koa-static');
const fs = require("fs");
const path = require("path");

app.use(static(`${process.cwd()}/static`));
app.use(bodyParser());
app.use(cors());

app.use(function(ctx, next){
    return next().catch((err) => {
        if ( err.status  == 401) {  
            ctx.status = 401;
            ctx.body = {
				code: 401,
                msg:"token失效"
            }
        } else {
            throw err;
        }
    });
});

app.use(koaJwt({
  secret: 'secret'
}).unless({
  path: [/\/login/,/\/register/,/\/visitor/,/\/notices/,/\/houses/,/\/rooms/]
}))

app.use(router.login.routes());
app.use(router.account.routes());
app.use(router.house.routes());
app.use(router.car.routes());
app.use(router.room.routes());
app.use(router.fee.routes());
app.use(router.check.routes());
app.use(router.notice.routes());
app.use(router.visitor.routes());
app.use(router.advice.routes());
app.listen(8080);